import java.util.Scanner;

public class DVDManager {

        DVD[] dvd = new DVD[6];

        //初始化
        void chushihuan() {
            dvd[0] = new DVD("诡秘之主", 1, 0, 18);
            dvd[1] = new DVD("完美世界", 0, 0, 22);
            dvd[2] = new DVD("悲惨世界", 0, 0, 10);
        }

        void qidong() {
            Scanner s = new Scanner(System.in);

            //菜单
            System.out.println("------------------------------");
            System.out.println("|-----欢迎使用 DVD租借器-----|");
            System.out.println("------------------------------");
            System.out.println("|           1.新增           |");
            System.out.println("|           2.查看           |");
            System.out.println("|           3.删除           |");
            System.out.println("|           4.租借           |");
            System.out.println("|           5.归还           |");
            System.out.println("|           6.退出           |");
            System.out.println("------------------------------");
            System.out.println("请选择：");
            int xuhao = s.nextInt();

            switch (xuhao) {
                case 1://新增
                    xinzeng();
                    fanhui();
                    break;
                case 2://查看
                    chakan();
                    fanhui();
                    break;
                case 3://删除
                    shanchu();
                    fanhui();
                    break;
                case 4://租借
                    zujie();
                    fanhui();
                    break;
                case 5://归还
                    guihuan();
                    fanhui();
                    break;
                case 6://退出
                    System.out.println("再见！");
                    break;
                default:
                    System.out.println("输入错误！");
            }

        }


        //返回方法
        void fanhui() {
            System.out.println("------------------------------------");
            System.out.println("返回输入0");
            Scanner s = new Scanner(System.in);
            //判断输入的是否为0
            if (s.nextInt() == 0) {
                qidong();
            } else {
                System.out.println("输入错误！");
                //失败的话重新调用，递归
                fanhui();
            }


        }

        //新增
        void xinzeng() {
            Scanner s = new Scanner(System.in);
            System.out.println("新增");
            System.out.print("请输入名称：");
            //获取输入的名称
            String name = s.next();
            //使用for循环查找内容为空的DVD
            for (int i = 0; i < dvd.length; i++) {
                //如果查找到为空的DVD 就将输入名称赋给它
                if (dvd[i] == null) {
                    dvd[i] = new DVD(name, 0, 0, 0);
                    System.out.println("成功！");
                    break;
                }
            }
        }

        //查看
        void chakan() {
            System.out.println("查看");
            System.out.println("序号\t状态\t名称\t\t日期\t次数");
            //循环遍历整个数组
            for (int i = 0; i < dvd.length; i++) {
                //如果不为空，将判断是否借出的属性转换为文本
                if (dvd[i] != null) {
                    String x = null;
                    //如果stata为1 便为已借出
                    if (dvd[i].getState() == 1) {
                        x = "已借出";
                    } else if (dvd[i].getState() == 0) {
                        x = "未借出";
                    }
                    //打印
                    System.out.println((i + 1) + "\t" + "\t" + x + "\t" + dvd[i].getName() + "\t" + dvd[i].getDate() + "号" + "\t" + "\t" + dvd[i].getCount() + "次");
                }
            }

        }

        //删除
        void shanchu() {
            Scanner s = new Scanner(System.in);
            System.out.println("删除");
            System.out.print("删除的名称：");
            //获得要删除的名称
            String name = s.next();
            //设一个o判断条件
            int o = -1;
            //遍历寻找名称对应的DVD
            for (int i = 0; i < dvd.length; i++) {
                if (dvd[i] != null) {
                    if (dvd[i].getName().equals(name)) {
                        o = i;
                        break;
                    }
                }
            }
            //如果找到对应DVD
            if (o != -1) {
                //如果已借出，则失败
                if (dvd[o].getState() == 1) {
                    System.out.println("DVD已借出，暂时不能删除.");
                } else if (dvd[o].getState() == 0) {
                    System.out.println("删除成功！");
                    dvd[o] = null;
                }
            } else if (o == -1) {//没有找到
                System.out.println("没有此DVD");
            }
        }

        void zujie() {
            Scanner s = new Scanner(System.in);
            System.out.println("租借");
            System.out.print("输入租借的名称：");
            String name = s.next();
            int o = -1;
            for (int i = 0; i < dvd.length; i++) {
                if (dvd[i] != null) {
                    if (dvd[i].getName().equals(name)) {
                        o = 1;
                        break;
                    }
                }
            }
            if (o != -1) {
                if (dvd[o].getState() == 1) {
                    System.out.println("DVD已借出.");
                } else if (dvd[o].getState() == 0) {
                    System.out.print("输入日期：");
                    int date = s.nextInt();
                    dvd[o].setDate(date);
                    dvd[o].setCount(dvd[o].getCount() + 1);
                    dvd[o].setState(1);
                    System.out.println("租借成功 ！");
                }

            } else if (o == -1) {
                System.out.println("没有此DVD");
            }

        }

        void guihuan() {
            Scanner s = new Scanner(System.in);
            System.out.println("归还");
            System.out.print("归还名称：");
            String name = s.next();
            int o = -1;
            for (int i = 0; i < dvd.length; i++) {
                if (dvd[i] != null) {
                    if (dvd[i].getName().equals(name)) {
                        o = 1;
                        break;
                    }
                }
            }
            if (o != -1) {
                if (dvd[o].getState() == 1) {
                    System.out.print("输入日期 ：");
                    int data = s.nextInt();
                    System.out.println("租金为：" + (data - dvd[o].getDate()) * 2 + "元");
                    dvd[o].setState(0);
                    dvd[o].setDate(0);
                } else if (dvd[o].getState() == 0) {
                    System.out.println("此DVD没有借出，归还失败");
                }

            } else if (o == -1) {
                System.out.println("没有此DVD");
            }

        }




}
